package com.wanmait.translate.system.controller;

import com.wanmait.translate.dto.ResultData;
import com.wanmait.translate.pojo.SystemDept;
import com.wanmait.translate.utils.CommonMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/system/dept")
public class SystemDeptController extends SystemParentController {
    private static final Logger LOG = LoggerFactory.getLogger(SystemDeptController.class);

    /*
     * @Author huweibin
     * @Description 查询所有部门
     * @Date 15:58 2020/11/8
     **/
    @RequestMapping("/list")
    public ResultData<SystemDept> getAll() {
        ResultData<SystemDept> res = null;
        try {
            res = ResultData.SuccesssWithData(systemDeptService.listSystemDept());
        } catch (Exception e) {
            e.printStackTrace();
            res = ResultData.fail(CommonMessage.ERR_MSG);
        }
        return res;
    }

    /*
     * @Author huweibin
     * @Description 模糊查询指定部门
     * @Date 15:58 2020/11/8
     **/
    @RequestMapping("/find")
    public ResultData<SystemDept> find(String deptName, Integer enable) {
        ResultData<SystemDept> res = null;
        LOG.info("查询参数为deptName：" + deptName + ",state:" + enable);
        try {
            res = ResultData.SuccesssWithData(systemDeptService.find(deptName, enable));
        } catch (Exception e) {
            e.printStackTrace();
            res = ResultData.fail(CommonMessage.ERR_MSG);
        }
        return res;
    }

    /*
     * @Author huweibin
     * @Description 新建和更新
     * @Date 15:58 2020/11/8
     **/
    @RequestMapping("/save")
    public void save(SystemDept systemDept) {

        //如果没传表示禁用
        if (systemDept.getEnable() == null) {
            systemDept.setEnable(1);
        }
        //没有id为新增
        if (systemDept.getId() == null) {
            systemDeptService.save(systemDept);
        } else {
            if (systemDept.getId().equals(systemDept.getParentId())) {
                return;
            }
            systemDeptService.update(systemDept);
        }
    }

    /*
     * @Author huweibin
     * @Description 移出部门
     * @Date 9:00 2020/11/11
     **/
    @RequestMapping("/remove")
    public ResultData remove(Integer id) {
        return systemDeptService.remove(id);
    }
}
